<template>
  <van-row>
    <van-nav-bar
      title="产品详情"
      left-arrow
      @click-left="$emit('close')"
    >
      <template #right>
        <van-popover
          v-model="showPopover"
          placement="bottom-end"
          trigger="click"
          :actions="actions"
          @select="handleAction"
        >
          <template #reference>
            <van-icon name="ellipsis" size="20" />
          </template>
        </van-popover>
      </template>
    </van-nav-bar>
    <van-cell title="模具名称" :value="submitForm.moldName" />
    <van-cell title="产品名称" :value="submitForm.productName" />
    <van-cell title="分类" :value="submitForm.categoryName" />
    <van-cell title="单价" :value="submitForm.unitPrice" />
    <van-cell title="单位" :value="submitForm.unit" />
    <van-cell title="颜色" :value="submitForm.color" />
    <van-cell title="材质" :value="submitForm.material" />
    <van-cell title="尺码" :value="submitForm.size" />
    <van-cell title="长度" :value="submitForm.length" />
    <van-cell title="重量" :value="submitForm.weight" />
    <van-cell title="库存" :value="submitForm.stock">
      <template #default>
        <div class="red">
          {{ submitForm.stock }}{{ submitForm.unit }}
        </div>
      </template>
    </van-cell>
    <van-popup v-model="showSubmit" position="right" :style="{ width: '100%', height: '100%' }">
      <product-submit-module ref="submitModule" @close="handleClose" />
    </van-popup>
  </van-row>
</template>
<script>
import { deleteProduct, selectProductById } from '@/api/product'
import ProductSubmitModule from '@/views/product/productSubmit.vue'
import { Dialog } from 'vant'

export default {
  name: 'ProductDetailModule',
  components: { ProductSubmitModule },
  data() {
    return {
      showPopover: false,
      showSubmit: false,
      submitForm: {},
      actions: [{ icon: 'edit', text: '修改', action: 'edit', disabled: false },
        { icon: 'delete-o', text: '删除', action: 'delete', disabled: false }]
    }
  },
  methods: {
    init(id) {
      selectProductById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      })
    },
    handleAction(row) {
      if (row.action === 'delete') {
        this.handleDelete()
      } else if (row.action === 'edit') {
        this.toEdit()
      }
    },
    handleDelete() {
      Dialog.confirm({
        title: '删除确认',
        message: '是否删除此信息'
      }).then(() => {
        deleteProduct({ ids: [this.submitForm.id] }).then(resp => {
          if (resp.success) {
            this.$toast.success('删除成功')
            this.$emit('close')
          }
        })
      }).catch(() => {})
    },
    toEdit() {
      this.showSubmit = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(this.submitForm.id)
      })
    },
    handleClose() {
      this.showSubmit = false
      this.init(this.submitForm.id)
    }
  }
}
</script>

<style scoped>

</style>
